﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using Eassets.Layered.Domain.Global;

namespace Eassets.Data.Global
{
    public class OrganizationLevelData
    {
        /// <summary>
        /// Add organizational level
        /// </summary>
        /// <param name="organizationLevel"></param>
        public void AddOrganizationLevel(OrganizationLevel organizationLevel)
        {
            SqlCommand command = new SqlCommand("SP_AddOrganizationLevel"); // Name of the SP
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, organizationLevel));
        }

        /// <summary>
        /// Updates organizationLevel.
        /// </summary>
        /// <param name="organizationLevel"></param>
        public void ModifyOrganizationLevel(OrganizationLevel organizationLevel)
        {
            SqlCommand command = new SqlCommand("SP_ModifyOrganizationLevel"); // Name of the SP
            command.Parameters.AddWithValue("@OrganizationLevelId", organizationLevel.OrganizationLevelId);
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, organizationLevel));
        }


        /// <summary>
        /// Delete organizationLevel.
        /// </summary>
        /// <param name="organizationLevelId"></param>
        public void DeleteOrganizationLevel(long organizationLevelId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteOrganizationLevel"); // Name of the SP
            command.Parameters.AddWithValue("@OrganizationLevelId", organizationLevelId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId organizationLevel.
        /// </summary>
        /// <param name="organizationLevelId"></param>
        /// <returns></returns>
        public OrganizationLevel GetByIdOrganizationLevel(long organizationLevelId)
        {
            SqlCommand command = new SqlCommand("SP_GetOrganizationLevelById");
            command.Parameters.AddWithValue("@OrganizationLevelId", organizationLevelId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            OrganizationLevel organizationLevel = new OrganizationLevel();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelId"].ToString())))
                {
                    organizationLevel.OrganizationLevelId = Convert.ToInt64(dataReader["OrganizationLevelId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    organizationLevel.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    organizationLevel.Description = Convert.ToString(dataReader["Description"]);
                }
            }
            dataReader.Close();
            return organizationLevel;
        }

        /// <summary>
        /// GetAll organizationLevel.
        /// </summary>
        /// <returns></returns>
        public Collection<OrganizationLevel> GetAllOrganizationLevel()
        {
            Collection<OrganizationLevel> organizationLevels = new Collection<OrganizationLevel>();
            SqlCommand command = new SqlCommand("SP_GetAllOrganizationLevels");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            OrganizationLevel organizationLevel;
            while (dataReader.Read())
            {
                organizationLevel = new OrganizationLevel();
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelId"].ToString())))
                {
                    organizationLevel.OrganizationLevelId = Convert.ToInt64(dataReader["OrganizationLevelId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    organizationLevel.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    organizationLevel.Description = Convert.ToString(dataReader["Description"]);
                }

                organizationLevels.Add(organizationLevel);
            }
            dataReader.Close();
            return organizationLevels;
        }

        public SqlCommand ConstructCommand(SqlCommand command, OrganizationLevel organizationLevel)
        {
            command.CommandType = CommandType.StoredProcedure;

            //try
            //{
            //    command.Parameters.AddWithValue("@OrganizationLevelId", organizationLevel.OrganizationLevelId);
            //}
            //catch (Exception)
            //{ }
            try
            {
                command.Parameters.AddWithValue("@Code", organizationLevel.Code);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Description", organizationLevel.Description);
            }
            catch (Exception)
            { }
            return command;
        }
    }
}

